﻿#region MIT Licence
/*
 * Copyright (C) 2014 PdfCreative
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 
 * and associated documentation files (the "Software"), to deal in the Software without restriction, 
 * including without limitation the rights to use, copy, modify, merge, publish, distribute, 
 * sublicense, and/or sell copies of the Software, and to permit persons to whom 
 * the Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included 
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE 
 * AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
#endregion

using System;
using System.Collections.Generic;
using System.Text;

namespace PdfCreative.Pdf.Graphics
{
    /// <summary>
    /// The ScopedResource class combines a resource, matrix and target Graphics boundaries. It represents the
    /// occurrence of a resource on a target whose coordinate system is transformed. It is used by the Graphics
    /// class to create multiple instances of a resource, valid within the target boundaries and on the
    /// appropriate coordinate system, when a resource is bound to these factors.
    /// It is not intended to be used directly.
    /// </summary>
    public class ScopedResource
    {
        // Private fields
        // ==============
        #region Private fields
        private IResourceWriter _resource;
        private Matrix _matrix;
        private int _matrixHash;
        private Rectangle _targetBounds;
        #endregion



        // Public properties
        // =================
        #region Public properties
        /// <summary>
        /// Retrieves the resource.
        /// </summary>
        public IResourceWriter Resource
        {
            get { return _resource; }
            set { _resource = value; }
        }

        /// <summary>
        /// Retrieves the transformation matrix.
        /// </summary>
        public Matrix CoordinateSpace
        {
            get { return _matrix; }
            set { _matrix = value; }
        }

        /// <summary>
        /// Retrieves the target bounds.
        /// </summary>
        public Rectangle TargetBounds
        {
            get { return _targetBounds; }
            set { _targetBounds = value; }
        }
        #endregion



        // Object overrides
        // ================
        #region Object overrides
        /// <summary>
        /// Indicates if this instance is equal to the specified other instance.
        /// </summary>
        /// <param name="obj">The other instance.</param>
        /// <returns>True if the two instances are equal; False otherwise.</returns>
        public override bool Equals(object obj)
        {
            ScopedResource other = obj as ScopedResource;

            bool matrixEquals = false;
            if (null == this.CoordinateSpace)
                matrixEquals = null == other.CoordinateSpace;
            else
                matrixEquals = this.CoordinateSpace.CompareValues(other.CoordinateSpace);

            return
                this.Resource.Equals(other.Resource) &&
                matrixEquals &&
                this.TargetBounds.Equals(other.TargetBounds);
        }

        /// <summary>
        /// Retrieves the hash code for this instance.
        /// </summary>
        /// <returns>The hash code</returns>
        public override int GetHashCode()
        {
            int hashCode = this.Resource.GetHashCode();
            if (null != this.CoordinateSpace)
                hashCode ^= _matrixHash;

            hashCode ^= this.TargetBounds.GetHashCode();
            
            return hashCode;
        }
        #endregion



        // Public constructor
        // ==================
        #region Public constructor
        /// <summary>
        /// Constructs a new ScopedResource instance for the specified resource, transformation matrix and
        /// target boundaries.
        /// </summary>
        /// <param name="resource">The resource</param>
        /// <param name="matrix">The transformation matrix</param>
        /// <param name="targetBounds">The target boundaries</param>
        public ScopedResource(IResourceWriter resource, Matrix matrix, Rectangle targetBounds)
        {
            if (null == resource)
                throw new ArgumentNullException("resource");

            _resource = resource;
            _matrix = matrix;
            if (null != _matrix)
                _matrixHash = _matrix.GetHashCode();

            _targetBounds = targetBounds;
        }

        /// <summary>
        /// Constructs a new ScopedResource instance based on the specified original instance.
        /// </summary>
        /// <param name="original">The original.</param>
        public ScopedResource(ScopedResource original)
        {
            if (null == original)
                throw new ArgumentNullException("original");

            _resource = original.Resource;
            if (null != original.CoordinateSpace)
            {
                _matrix = (Matrix)original.CoordinateSpace.Clone();
                _matrixHash = original._matrixHash;
            }
            
            _targetBounds = original.TargetBounds;
        }
        #endregion

    } // ScopedResource class
}
